package chen;
import chen.toolbean.MyTools;
import chen.valuebean.GoodsSingle;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(name = "ShopcarServlet", urlPatterns = {"/Shopcar"})
public class ShopcarServlet extends HttpServlet {
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
     HttpSession session = request.getSession();
     response.setContentType("text/html;charset=UTF-8");
     try (PrintWriter out = response.getWriter()) 
    {
      /* TODO output your page here. You may use following sample code. */
      out.println("<!DOCTYPE html>");
      out.println("<html>");
      out.println("<head>ShopCar类实例</head>");        
      out.write("<body>");
      out.write("<!-- 我的购物车 -->\n");
      chen.toolbean.ShopCar myCar;
      synchronized (session) {
        myCar = (chen.toolbean.ShopCar) session.getAttribute("myCar");
        if (myCar == null)
        {
          myCar = new chen.toolbean.ShopCar();
          session.setAttribute("myCar", myCar);
        }
      }
      //获取实例中用来存储购买的商品的集合
      ArrayList buylist=myCar.getBuylist();
      float total=0;//用来存储应付金额
      out.write("\n");
      out.write("\n");
      out.write("<table border=\"1\" width=\"450\" rules=\"none\" cellspacing=\"0\" cellpadding=\"0\">\n");
      out.write("\t<tr height=\"50\"><td colspan=\"5\" align=\"center\">购买的商品如下</td></tr>\n");
      out.write("\t<tr align=\"center\" height=\"30\" bgcolor=\"lightgrey\">\n");
      out.write("\t\t<td width=\"25%\">名称</td>\n");
      out.write("\t\t<td>价格(元/斤)</td>\n");
      out.write("\t\t<td>数量</td>\n");
      out.write("\t\t<td>总价(元)</td>\n");
      out.write("\t\t<td>移除(-1/次)</td>\n");
      out.write("\t</tr>\n");
      out.write("\t");
      if(buylist==null||buylist.isEmpty())
      { 
         out.write("\n");
         out.write("\t<tr height=\"100\"><td colspan=\"5\" align=\"center\">您的购物车为空！</td></tr>\n");
         out.write("\t");
      }else
      {
	for(int i=0;i<buylist.size();i++)
        {
	 GoodsSingle single=(GoodsSingle)buylist.get(i);
	 String name=single.getName();							//获取商品名称
	 float price=single.getPrice();							//获取商品价格
	 int num=single.getNum();								//获取购买数量				
	 float money=((int)((price*num+0.05f)*10))/10f;
         //计算当前商品总价，并进行四舍五入
	 total+=money;											//计算应付金额	
         out.write("\n");
         out.write("\t<tr align=\"center\" height=\"50\">\n");
         out.write("\t\t<td>");
         out.print(name);
         out.write("</td>\n");
         out.write("\t\t<td>");
         out.print(price);
         out.write("</td>\n");
         out.write("\t\t<td>");
         out.print(num);
         out.write("</td>\n");
         out.write("\t\t<td>");
         out.print(money);
         out.write("</td>\n");
         out.write("\t\t<td>\n");
         out.write("<a href=\"Docar?action=remove&name=");
         out.print(MyTools.encode(single.getName()));
         out.write("\" >移除</a>\n");
         out.write("</td>\n");
         out.write("\t</tr>\n");
         out.write("\t");				
	}
       }	
      out.write("\n");
      out.write("\t<tr height=\"50\" align=\"center\"><td colspan=\"5\">应付金额：");
      out.print(total);
      out.write("</td></tr>\n");
      out.write("\t<tr height=\"50\" align=\"center\">\n");
      out.write("\t\t<td colspan=\"2\"><a href=\"Show\">继续购物</a></td>\n");
      out.write("\t\t<td colspan=\"3\"><a href=\"Docar?action=clear\">清空购物车</a></td>\n");
      out.write("\t</tr>\t\t\t\t\n");
      out.write("</table>");
      out.println("</body>");
      out.println("</html>");
    }
   }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
